package cn.liccshop.sys.base;

import cn.liccshop.core.utils.QiniuUtils;
import cn.liccshop.sys.cache.CommonCacheUtil;
import cn.liccshop.sys.constants.Constants;
import cn.liccshop.sys.entity.Result;
import cn.liccshop.sys.entity.UserElement;
import cn.liccshop.sys.exception.MySystemException;
import com.baomidou.mybatisplus.extension.service.IService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @program LiCCShop
 * @author LiCC
 * @since 2019-03-15-22:51
 */
@Slf4j
public class BaseController<T> {

    /**
     * 添加数据
     */
    protected static final String L_DATA_UPLOADFILE = "/data/uploadFile";

    @Autowired
    private CommonCacheUtil commonCacheUtil;

    protected IService<T> getLiService(){
        return null;
    }
    protected IService<T> theService() throws MySystemException {
        IService service = this.getLiService();
        if (null == service){
            throw new MySystemException("BaseService isn't found!", 104);
        }
        return service;
    }

    /**
     * 通过该方法Controller可以获取用户信息
     * @return
     */
    protected UserElement getUserInfoByToken(){
        HttpServletRequest servletRequest = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        String token = servletRequest.getHeader(Constants.REQUEST_TOKEN_KEY);
        if (!StringUtils.isBlank(token)){
            try {
                UserElement userElement = commonCacheUtil.getUserElementByToken(token);
                userElement.setToken(token);
                return userElement;
            } catch (Exception e){
                log.error("通过token获取用户在缓存中的信息失败", e);
                throw e;
            }
        }
        return null;
    }


    /**
     * 获取七牛云的配置
     * @return
     * @throws MySystemException
     */
    @RequestMapping(L_DATA_UPLOADFILE)
    public Result uploadFile(@RequestParam("file") MultipartFile[] files) throws MySystemException {
        Map<String, String> data = QiniuUtils.getQiniuInfo(files);
        return new Result().setCode(Constants.OK).setData(data).setMessage("上传成功");
    }

    /**
     * 获取用户真正的IP地址
     * 反向代理应该都可以查出来
     * @param request
     * @return
     */
    protected String getIpFromRequest(HttpServletRequest request){
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unkonw".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unkonw".equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unkonw".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
    }
}
